package cmpe273.classproject.testclient;

import java.rmi.RemoteException;
import java.util.Calendar;
import java.util.Random;

import cmpe273.classproject.server.dto.MovieResponse;
import cmpe273.classproject.server.dto.PersonResponse;
import cmpe273.classproject.server.model.Movie;
import cmpe273.classproject.server.model.Person;
import cmpe273.classproject.server.service.MovieServiceImplProxy;

public class LoadTest {

	public MovieServiceImplProxy service = new MovieServiceImplProxy();

	public static void main(String... args) throws RemoteException, InterruptedException {
		new LoadTest().runTest();
	}
	
	public void runTest() throws RemoteException, InterruptedException {
		 System.out.println("Try 2000 consecutive inserts of Movie-object...");
		long startTime = System.currentTimeMillis();
		 this.insertMovie(2000);
		long endTime = System.currentTimeMillis();
		 System.out.println("Total time spent: " + (endTime - startTime)/1000
		 + " s");
		 System.out.println("Average time pr operation: " +(endTime -
		 startTime)/2000 + "ms");
		 System.out.println();
		 System.out.println("Try 2000 consecutive queries of Movie-object...");
		 startTime = System.currentTimeMillis();
		 this.queryMovie(2000);
		 endTime = System.currentTimeMillis();
		 System.out.println("Total time spent: " + (endTime - startTime)/1000
		 + " s");
		 System.out.println("Average time pr operation: " +(endTime -
		 startTime)/2000 + "ms");
		
		//Multithreaded reads from database
		System.out.println("Accessing 50 random movies 10000 times... (cached)");
		startTime = System.currentTimeMillis();
		this.testRandomAccess();
		endTime = System.currentTimeMillis();
		System.out.println("Total time spent: " + (endTime - startTime) + " ms");
		System.out.println();
		
//		//Check that various services are working
		System.out.println("Test getAllMoviesByPerson");
		System.out.println("Movies returned: " +service.getAllMoviesByPerson(12).getMovies().length);
		System.out.println();
		System.out.println("Test getAllPersons");
		System.out.println("Persons returned: " +service.getAllPersons().getPersons().length);
		
		System.out.println();
		Movie movie = new Movie();
		movie.setMovieId(12);
		movie.setAvailableCopy(-1);
		movie.setTotalCopies(-1);
		MovieResponse mr = null;
		
		System.out.println("Test updateMovie");
		movie.setMovieName("Changed name ZYX");
		mr = service.updateMovie(movie);
		if(mr.getError() == 0) {
			System.out.println("Movie update succesfully.");
		} else {
			System.out.println("Movie update failed: " + mr.getErrorMessage());
		}
		
		System.out.println();
		System.out.println("Test getAllOutstandingMoviesByPerson");
		mr = service.getAllOutstandingMoviesByPerson(12);
		if(mr.getError() == 0) {
			System.out.println("Fetch movies was succesful: " + mr.getMovies().length);
		} else {
			System.out.println("Movie update failed: " + mr.getErrorMessage());
		}
		System.out.println();
		System.out.println("Test getAllPersons");
		PersonResponse pr = service.getAllPersons();
		if(pr.getError() == 0) {
			System.out.println("Fetch persons was succesful: " + pr.getPersons().length);
		} else {
			System.out.println("Movie update failed: " + pr.getErrorMessage());
		}
		
	}

	public void testRandomAccess() throws RemoteException, InterruptedException {
		MyThread[] myThreads = new MyThread[10000];
		for (int i = 0; i < 10000; i++) {
			myThreads[i] = new MyThread(String.valueOf(i));
			myThreads[i].t.join();
		}
	}

	public void queryMovie(int amount) throws RemoteException {
		for (int i = 0; i < amount; i++) {
			Movie m = new Movie();
			m.setMovieId(12);
			service.findMovie(m);
		}
	}

	public void insertMovie(int amount) throws RemoteException {

		for (int i = 0; i < amount; i++) {
			Movie m = new Movie();
			m.setTotalCopies(200);
			m.setMovieBanner("There are no clean getaways");
			m.setMovieCategory("action");
			m.setMovieName(i + " No country for old men");
			m.setRentAmount(10);
			m.setReleaseDate(Calendar.getInstance());
			service.createMovie(m);

		}
	}
}
